package com.adingxiong.pm.handler;


import com.adingxiong.pm.util.TokenUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置token拦截器
 */
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //查看访问路径
        /*String pathUrl = request.getRequestURI();
        if(pathUrl.equals("/pm/doc.html") || pathUrl.equals("/pm/user/login")
                || pathUrl.contains("/pm/webjars/" )|| pathUrl.equals("/pm/v2/api-docs")
                || pathUrl.contains("/pm/swagger")
        ){
            return true;
        }
        //获取token
        String token = request.getHeader(TokenUtils.tokenHeader);
        if(token ==null){
            //response.sendRedirect("/pm/user/login");
            response.getWriter().append("<h1 style='text-align:center;'>Not allowed! please login and try </h1>");
            return false;
        }
        try {
            TokenUtils.getTokenBody(token);

        } catch (Exception e){
            e.printStackTrace();
            //response.sendRedirect("/pm/user/login");
            response.getWriter().append("<h1 style='text-align:center;color:red'>system error! please try  later</h1>");
            return false;
        }*/
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
